package com.jadefashion.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.mapper.CheckGroupMapper;
import com.jadefashion.pojo.CheckGroup;
import com.jadefashion.pojo.CheckItem;
import com.jadefashion.pojo.MulitObjectBean;
import com.jadefashion.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    @Override
    public void updateCheckGroup(MulitObjectBean mulitObjectBean) {
        CheckGroup checkGroup = mulitObjectBean.getFormData();
        Integer[] ids = mulitObjectBean.getCheckitemIds();
        //更改检查组数据
        checkGroupMapper.update(checkGroup);
        //先解除关系多对多
        checkGroupMapper.deleteCheckItemIdByCheckGroupId(checkGroup.getId());
        //再绑定关系多对多
		
        //for (Integer id : ids) {
        //    checkGroupMapper.addCheckGroupIdAndCheckItemIds(checkGroup.getId(),id);
        //}
		checkGroupMapper.addCheckGroupIdAndCheckItemIds(checkGroup.getId(), Arrays.asList(ids));
    }

    @Override
    public Integer[] findCheckItemIdsByGroupId(Integer id) {
        Integer[] ids= checkGroupMapper.findCheckItemIdsByGroupId(id);
        return ids;
    }

    @Override
    public void deleteCheckitemById(Integer id) {
        checkGroupMapper.deleteCheckItemIdByCheckGroupId(id);
        checkGroupMapper.deleteCheckGroupById(id);
    }

    @Override
    public Map<String, Object> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<CheckGroup> list= checkGroupMapper.findPage(queryPageBean.getQueryString());
        PageInfo<CheckGroup> checkItemPageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",checkItemPageInfo.getTotal());
        map.put("list",checkItemPageInfo.getList());
        return map;
    }

    @Override
    public void addCheckGroup(MulitObjectBean map) {
        CheckGroup checkGroup = map.getFormData();
        Integer[] ids =  map.getCheckitemIds();
       checkGroupMapper.addCheckGroup(checkGroup);
//        for (Integer id : ids) {
//            checkGroupMapper.addCheckGroupIdAndCheckItemIds(checkGroup.getId(),id);
//        }
        checkGroupMapper.addCheckGroupIdAndCheckItemIds(checkGroup.getId(), Arrays.asList(ids));
    }
}
